//
//  RootViewController.m
//  UI9_限时代码
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "Student.h"
#import "DetailViewController.h"
#import "AddViewController.h"
#import "ChineseToPinyin.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, AddViewControllerDelegate>

@property (nonatomic, retain) NSMutableDictionary *stuDic;
@property (nonatomic, retain) UITableView *tableV;
//@property (nonatomic, retain) UITableViewCell *cell;

@end

@implementation RootViewController


- (void)dealloc
{
    [_stuDic release];
    [_tableV release];
//    [_cell release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"通讯录";
    
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction:)];
    
    
}

// 实现协议方法
- (void)addStudent:(Student *)stu
{
    if (stu != nil) {
        
        NSString *key = [[ChineseToPinyin pinyinFromChiniseString:stu.name] substringToIndex:1];
        NSMutableArray *arr = nil;
        if ([self.stuDic objectForKey:key] != nil) {
            arr = [self.stuDic objectForKey:key];
            [arr addObject:stu];
        } else {
            arr = [NSMutableArray array];
            [arr addObject:stu];
            [self.stuDic setObject:arr forKey:key];
        }
            [self.tableV reloadData];
    }

    
}


- (void)leftAction:(UIBarButtonItem *)sender
{
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];
    
}


// 选中行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    NSString *key = [self.stuDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    detailVC.stu = [arr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];

}



// 移动监测
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
        return sourceIndexPath;
    } else {
        return proposedDestinationIndexPath;
    }
    

}

// 移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *key = [self.stuDic.allKeys objectAtIndex:sourceIndexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    Student *stu = [[arr objectAtIndex:sourceIndexPath.row] retain];
    [arr removeObjectAtIndex:sourceIndexPath.row];
    
    NSString *desKey = [self.stuDic.allKeys objectAtIndex:destinationIndexPath.section];
    NSMutableArray *desArr = [self.stuDic objectForKey:desKey];
    [desArr addObject:stu];
    [stu release];
    
    [self.tableV reloadData];
    
}




// 状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
}

// 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

// 编辑完成
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.stuDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    

    
    [arr removeObjectAtIndex:indexPath.row];
    
    if (arr.count == 0) {
        [self.stuDic removeObjectForKey:key];
    }
    [self.tableV reloadData];
    
    
    
}


// 返回可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 返回可移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.stuDic.allKeys;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.stuDic.allKeys objectAtIndex:section];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.stuDic.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.stuDic.allKeys objectAtIndex:section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    return arr.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [self.tableV dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
        
    }
    NSString *key = [self.stuDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    Student *stu = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = stu.name;
    cell.detailTextLabel.text = stu.phone;
    cell.imageView.image = [UIImage imageNamed:stu.image];
    return cell;
    
    
    
    
    
}


- (void)getData
{
    NSString *fileName = [[NSBundle mainBundle] pathForResource:@"HW" ofType:@"plist"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:fileName];
    
    self.stuDic = [NSMutableDictionary dictionary];
    
    for (NSString *key in dic) {
        
        NSMutableArray *stuArr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in [dic objectForKey:key]) {
            
            Student *stu = [[Student alloc] init];
            [stu setValuesForKeysWithDictionary:tempDic];
            [stuArr addObject:stu];
            [stu release];
        }
        [self.stuDic setObject:stuArr forKey:key];
        
    }

    
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
